.class public Lcom/facebook/orca/protocol/RealBatchRunner;
.super Lcom/facebook/orca/protocol/BatchRunner;
.source "RealBatchRunner.java"


# instance fields
.field private final a:Lcom/facebook/orca/config/OrcaHttpConfig;

.field private final b:Lcom/facebook/orca/common/http/OrcaHttpRequestProcessor;

.field private final c:Lcom/facebook/orca/auth/AuthenticationManager;

.field private final d:Z


# direct methods
.method public constructor <init>(Lcom/facebook/orca/config/OrcaHttpConfig;Lcom/facebook/orca/common/http/OrcaHttpRequestProcessor;Lcom/facebook/orca/auth/AuthenticationManager;Z)V
    .locals 0
    .parameter
    .end parameter
    .parameter
    .end parameter
    .parameter
    .end parameter
    .parameter
        .annotation runtime Lcom/facebook/orca/annotations/PhpProfiling;
        .end annotation
    .end parameter
    .annotation runtime Lcom/google/inject/Inject;
    .end annotation

    .prologue
    .line 46
    invoke-direct {p0}, Lcom/facebook/orca/protocol/BatchRunner;-><init>()V

    .line 47
    iput-object p1, p0, Lcom/facebook/orca/protocol/RealBatchRunner;->a:Lcom/facebook/orca/config/OrcaHttpConfig;

    .line 48
    iput-object p2, p0, Lcom/facebook/orca/protocol/RealBatchRunner;->b:Lcom/facebook/orca/common/http/OrcaHttpRequestProcessor;

    .line 49
    iput-object p3, p0, Lcom/facebook/orca/protocol/RealBatchRunner;->c:Lcom/facebook/orca/auth/AuthenticationManager;

    .line 50
    iput-boolean p4, p0, Lcom/facebook/orca/protocol/RealBatchRunner;->d:Z

    .line 51
    return-void
.end method


# virtual methods
.method public b(Ljava/lang/String;)V
    .locals 11
    .parameter

    .prologue
    const/4 v10, 0x0

    .line 54
    new-instance v1, Lorg/json/JSONArray;

    invoke-direct {v1}, Lorg/json/JSONArray;-><init>()V

    .line 55
    invoke-virtual {p0}, Lcom/facebook/orca/protocol/RealBatchRunner;->a()Ljava/util/List;

    move-result-object v2

    .line 56
    invoke-static {}, Lcom/google/common/collect/Lists;->a()Ljava/util/ArrayList;

    move-result-object v3

    .line 57
    invoke-interface {v2}, Ljava/util/List;->iterator()Ljava/util/Iterator;

    move-result-object v4

    :goto_0
    invoke-interface {v4}, Ljava/util/Iterator;->hasNext()Z

    move-result v0

    if-eqz v0, :cond_6

    invoke-interface {v4}, Ljava/util/Iterator;->next()Ljava/lang/Object;

    move-result-object v0

    check-cast v0, Lcom/facebook/orca/protocol/BatchOperation;

    .line 58
    invoke-virtual {v0}, Lcom/facebook/orca/protocol/BatchOperation;->a()Lcom/facebook/orca/protocol/ApiMethod;

    move-result-object v5

    .line 59
    invoke-virtual {v0}, Lcom/facebook/orca/protocol/BatchOperation;->b()Ljava/lang/Object;

    move-result-object v6

    invoke-interface {v5, v6}, Lcom/facebook/orca/protocol/ApiMethod;->a(Ljava/lang/Object;)Lcom/facebook/orca/protocol/ApiRequest;

    move-result-object v5

    .line 60
    new-instance v6, Lorg/json/JSONObject;

    invoke-direct {v6}, Lorg/json/JSONObject;-><init>()V

    .line 61
    const-string v7, "method"

    invoke-virtual {v5}, Lcom/facebook/orca/protocol/ApiRequest;->b()Ljava/lang/String;

    move-result-object v8

    invoke-virtual {v6, v7, v8}, Lorg/json/JSONObject;->put(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;

    .line 63
    invoke-virtual {v5}, Lcom/facebook/orca/protocol/ApiRequest;->c()Ljava/lang/String;

    move-result-object v7

    .line 64
    const-string v8, "GET"

    invoke-virtual {v5}, Lcom/facebook/orca/protocol/ApiRequest;->b()Ljava/lang/String;

    move-result-object v9

    invoke-virtual {v8, v9}, Ljava/lang/String;->equals(Ljava/lang/Object;)Z

    move-result v8

    if-eqz v8, :cond_3

    .line 65
    invoke-virtual {v5}, Lcom/facebook/orca/protocol/ApiRequest;->d()Lcom/google/common/collect/ImmutableList;

    move-result-object v8

    const-string v9, "UTF-8"

    invoke-static {v8, v9}, Lorg/apache/http/client/utils/URLEncodedUtils;->format(Ljava/util/List;Ljava/lang/String;)Ljava/lang/String;

    move-result-object v8

    .line 66
    new-instance v9, Ljava/lang/StringBuilder;

    invoke-direct {v9}, Ljava/lang/StringBuilder;-><init>()V

    invoke-virtual {v9, v7}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v7

    const-string v9, "?"

    invoke-virtual {v7, v9}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v7

    invoke-virtual {v7, v8}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v7

    invoke-virtual {v7}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

    move-result-object v7

    .line 76
    :goto_1
    invoke-virtual {v0}, Lcom/facebook/orca/protocol/BatchOperation;->c()Ljava/lang/String;

    move-result-object v8

    if-eqz v8, :cond_0

    .line 77
    const-string v8, "name"

    invoke-virtual {v0}, Lcom/facebook/orca/protocol/BatchOperation;->c()Ljava/lang/String;

    move-result-object v9

    invoke-virtual {v6, v8, v9}, Lorg/json/JSONObject;->put(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;

    .line 79
    :cond_0
    invoke-virtual {v0}, Lcom/facebook/orca/protocol/BatchOperation;->d()Ljava/lang/String;

    move-result-object v8

    if-eqz v8, :cond_1

    .line 80
    const-string v8, "depends_on"

    invoke-virtual {v0}, Lcom/facebook/orca/protocol/BatchOperation;->d()Ljava/lang/String;

    move-result-object v0

    invoke-virtual {v6, v8, v0}, Lorg/json/JSONObject;->put(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;

    .line 82
    :cond_1
    invoke-virtual {v5}, Lcom/facebook/orca/protocol/ApiRequest;->e()Lorg/apache/http/entity/mime/FormBodyPart;

    move-result-object v0

    if-eqz v0, :cond_2

    .line 83
    const-string v0, "attached_files"

    invoke-virtual {v5}, Lcom/facebook/orca/protocol/ApiRequest;->e()Lorg/apache/http/entity/mime/FormBodyPart;

    move-result-object v8

    invoke-virtual {v8}, Lorg/apache/http/entity/mime/FormBodyPart;->a()Ljava/lang/String;

    move-result-object v8

    invoke-virtual {v6, v0, v8}, Lorg/json/JSONObject;->put(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;

    .line 84
    invoke-virtual {v5}, Lcom/facebook/orca/protocol/ApiRequest;->e()Lorg/apache/http/entity/mime/FormBodyPart;

    move-result-object v0

    invoke-interface {v3, v0}, Ljava/util/List;->add(Ljava/lang/Object;)Z

    .line 86
    :cond_2
    const-string v0, "omit_response_on_success"

    invoke-virtual {v6, v0, v10}, Lorg/json/JSONObject;->put(Ljava/lang/String;Z)Lorg/json/JSONObject;

    .line 87
    const-string v0, "relative_url"

    invoke-virtual {v6, v0, v7}, Lorg/json/JSONObject;->put(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;

    .line 88
    invoke-virtual {v1, v6}, Lorg/json/JSONArray;->put(Ljava/lang/Object;)Lorg/json/JSONArray;

    goto/16 :goto_0

    .line 67
    :cond_3
    const-string v8, "POST"

    invoke-virtual {v5}, Lcom/facebook/orca/protocol/ApiRequest;->b()Ljava/lang/String;

    move-result-object v9

    invoke-virtual {v8, v9}, Ljava/lang/String;->equals(Ljava/lang/Object;)Z

    move-result v8

    if-eqz v8, :cond_4

    .line 68
    invoke-virtual {v5}, Lcom/facebook/orca/protocol/ApiRequest;->d()Lcom/google/common/collect/ImmutableList;

    move-result-object v8

    const-string v9, "UTF-8"

    invoke-static {v8, v9}, Lorg/apache/http/client/utils/URLEncodedUtils;->format(Ljava/util/List;Ljava/lang/String;)Ljava/lang/String;

    move-result-object v8

    .line 69
    const-string v9, "body"

    invoke-virtual {v6, v9, v8}, Lorg/json/JSONObject;->put(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;

    goto :goto_1

    .line 70
    :cond_4
    const-string v8, "DELETE"

    invoke-virtual {v5}, Lcom/facebook/orca/protocol/ApiRequest;->b()Ljava/lang/String;

    move-result-object v9

    invoke-virtual {v8, v9}, Ljava/lang/String;->equals(Ljava/lang/Object;)Z

    move-result v8

    if-eqz v8, :cond_5

    .line 71
    invoke-virtual {v5}, Lcom/facebook/orca/protocol/ApiRequest;->d()Lcom/google/common/collect/ImmutableList;

    move-result-object v8

    const-string v9, "UTF-8"

    invoke-static {v8, v9}, Lorg/apache/http/client/utils/URLEncodedUtils;->format(Ljava/util/List;Ljava/lang/String;)Ljava/lang/String;

    move-result-object v8

    .line 72
    new-instance v9, Ljava/lang/StringBuilder;

    invoke-direct {v9}, Ljava/lang/StringBuilder;-><init>()V

    invoke-virtual {v9, v7}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v7

    const-string v9, "?"

    invoke-virtual {v7, v9}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v7

    invoke-virtual {v7, v8}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v7

    invoke-virtual {v7}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

    move-result-object v7

    goto/16 :goto_1

    .line 74
    :cond_5
    new-instance v0, Ljava/lang/Exception;

    new-instance v1, Ljava/lang/StringBuilder;

    invoke-direct {v1}, Ljava/lang/StringBuilder;-><init>()V

    const-string v2, "Unsupported method: "

    invoke-virtual {v1, v2}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v1

    invoke-virtual {v5}, Lcom/facebook/orca/protocol/ApiRequest;->b()Ljava/lang/String;

    move-result-object v2

    invoke-virtual {v1, v2}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v1

    invoke-virtual {v1}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

    move-result-object v1

    invoke-direct {v0, v1}, Ljava/lang/Exception;-><init>(Ljava/lang/String;)V

    throw v0

    .line 91
    :cond_6
    new-instance v4, Lorg/apache/http/entity/mime/MultipartEntity;

    invoke-direct {v4}, Lorg/apache/http/entity/mime/MultipartEntity;-><init>()V

    .line 92
    iget-object v0, p0, Lcom/facebook/orca/protocol/RealBatchRunner;->c:Lcom/facebook/orca/auth/AuthenticationManager;

    invoke-virtual {v0}, Lcom/facebook/orca/auth/AuthenticationManager;->a()Lcom/facebook/orca/auth/FacebookCredentials;

    move-result-object v0

    invoke-virtual {v0}, Lcom/facebook/orca/auth/FacebookCredentials;->a()Ljava/lang/String;

    move-result-object v0

    .line 93
    const-string v5, "access_token"

    new-instance v6, Lorg/apache/http/entity/mime/content/StringBody;

    invoke-direct {v6, v0}, Lorg/apache/http/entity/mime/content/StringBody;-><init>(Ljava/lang/String;)V

    invoke-virtual {v4, v5, v6}, Lorg/apache/http/entity/mime/MultipartEntity;->a(Ljava/lang/String;Lorg/apache/http/entity/mime/content/ContentBody;)V

    .line 94
    invoke-virtual {v1}, Lorg/json/JSONArray;->toString()Ljava/lang/String;

    move-result-object v0

    .line 95
    const-string v1, "RealBatchRunner"

    const/4 v5, 0x2

    invoke-static {v1, v5}, Lcom/facebook/orca/debug/BLog;->a(Ljava/lang/String;I)Z

    move-result v1

    if-eqz v1, :cond_7

    .line 96
    const-string v1, "RealBatchRunner"

    new-instance v5, Ljava/lang/StringBuilder;

    invoke-direct {v5}, Ljava/lang/StringBuilder;-><init>()V

    const-string v6, "Request JSON: "

    invoke-virtual {v5, v6}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v5

    invoke-virtual {v5, v0}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v5

    invoke-virtual {v5}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

    move-result-object v5

    invoke-static {v1, v5}, Lcom/facebook/orca/debug/BLog;->a(Ljava/lang/String;Ljava/lang/String;)V

    .line 98
    :cond_7
    const-string v1, "batch"

    new-instance v5, Lorg/apache/http/entity/mime/content/StringBody;

    invoke-direct {v5, v0}, Lorg/apache/http/entity/mime/content/StringBody;-><init>(Ljava/lang/String;)V

    invoke-virtual {v4, v1, v5}, Lorg/apache/http/entity/mime/MultipartEntity;->a(Ljava/lang/String;Lorg/apache/http/entity/mime/content/ContentBody;)V

    .line 99
    invoke-interface {v3}, Ljava/util/List;->iterator()Ljava/util/Iterator;

    move-result-object v1

    :goto_2
    invoke-interface {v1}, Ljava/util/Iterator;->hasNext()Z

    move-result v0

    if-eqz v0, :cond_8

    invoke-interface {v1}, Ljava/util/Iterator;->next()Ljava/lang/Object;

    move-result-object v0

    check-cast v0, Lorg/apache/http/entity/mime/FormBodyPart;

    .line 100
    invoke-virtual {v0}, Lorg/apache/http/entity/mime/FormBodyPart;->a()Ljava/lang/String;

    move-result-object v3

    invoke-virtual {v0}, Lorg/apache/http/entity/mime/FormBodyPart;->c()Lorg/apache/james/mime4j/message/Body;

    move-result-object v0

    check-cast v0, Lorg/apache/http/entity/mime/content/ContentBody;

    invoke-virtual {v4, v3, v0}, Lorg/apache/http/entity/mime/MultipartEntity;->a(Ljava/lang/String;Lorg/apache/http/entity/mime/content/ContentBody;)V

    goto :goto_2

    .line 103
    :cond_8
    iget-object v0, p0, Lcom/facebook/orca/protocol/RealBatchRunner;->a:Lcom/facebook/orca/config/OrcaHttpConfig;

    invoke-interface {v0}, Lcom/facebook/orca/config/OrcaHttpConfig;->b()Landroid/net/Uri$Builder;

    move-result-object v0

    .line 104
    iget-boolean v1, p0, Lcom/facebook/orca/protocol/RealBatchRunner;->d:Z

    if-eqz v1, :cond_9

    .line 105
    const-string v1, "hotprofiler"

    const-string v3, "3"

    invoke-virtual {v0, v1, v3}, Landroid/net/Uri$Builder;->appendQueryParameter(Ljava/lang/String;Ljava/lang/String;)Landroid/net/Uri$Builder;

    .line 106
    const-string v1, "hotprofiler_ajax"

    const-string v3, "1"

    invoke-virtual {v0, v1, v3}, Landroid/net/Uri$Builder;->appendQueryParameter(Ljava/lang/String;Ljava/lang/String;)Landroid/net/Uri$Builder;

    .line 107
    iget-object v1, p0, Lcom/facebook/orca/protocol/RealBatchRunner;->c:Lcom/facebook/orca/auth/AuthenticationManager;

    invoke-virtual {v1}, Lcom/facebook/orca/auth/AuthenticationManager;->f()Lcom/facebook/orca/users/User;

    move-result-object v1

    .line 108
    if-eqz v1, :cond_9

    .line 109
    const-string v3, "phprof_user"

    invoke-virtual {v1}, Lcom/facebook/orca/users/User;->b()Ljava/lang/String;

    move-result-object v1

    invoke-virtual {v0, v3, v1}, Landroid/net/Uri$Builder;->appendQueryParameter(Ljava/lang/String;Ljava/lang/String;)Landroid/net/Uri$Builder;

    .line 112
    :cond_9
    new-instance v1, Lorg/apache/http/client/methods/HttpPost;

    invoke-virtual {v0}, Landroid/net/Uri$Builder;->build()Landroid/net/Uri;

    move-result-object v0

    invoke-virtual {v0}, Landroid/net/Uri;->toString()Ljava/lang/String;

    move-result-object v0

    invoke-direct {v1, v0}, Lorg/apache/http/client/methods/HttpPost;-><init>(Ljava/lang/String;)V

    .line 113
    invoke-virtual {v1, v4}, Lorg/apache/http/client/methods/HttpPost;->setEntity(Lorg/apache/http/HttpEntity;)V

    .line 115
    new-instance v0, Lcom/facebook/orca/protocol/CustomResponseHandler;

    invoke-direct {v0}, Lcom/facebook/orca/protocol/CustomResponseHandler;-><init>()V

    .line 116
    iget-object v3, p0, Lcom/facebook/orca/protocol/RealBatchRunner;->b:Lcom/facebook/orca/common/http/OrcaHttpRequestProcessor;

    invoke-virtual {v3, p1, v1, v0}, Lcom/facebook/orca/common/http/OrcaHttpRequestProcessor;->a(Ljava/lang/String;Lorg/apache/http/client/methods/HttpUriRequest;Lorg/apache/http/client/ResponseHandler;)Ljava/lang/Object;

    move-result-object v0

    check-cast v0, Ljava/lang/String;

    .line 118
    new-instance v1, Lorg/json/JSONArray;

    new-instance v3, Lorg/json/JSONTokener;

    invoke-direct {v3, v0}, Lorg/json/JSONTokener;-><init>(Ljava/lang/String;)V

    invoke-direct {v1, v3}, Lorg/json/JSONArray;-><init>(Lorg/json/JSONTokener;)V

    .line 119
    invoke-virtual {v1}, Lorg/json/JSONArray;->length()I

    move-result v0

    invoke-interface {v2}, Ljava/util/List;->size()I

    move-result v3

    if-eq v0, v3, :cond_a

    .line 120
    new-instance v0, Ljava/lang/Exception;

    const-string v1, "Received wrong number of batches in response"

    invoke-direct {v0, v1}, Ljava/lang/Exception;-><init>(Ljava/lang/String;)V

    throw v0

    :cond_a
    move v3, v10

    .line 123
    :goto_3
    invoke-virtual {v1}, Lorg/json/JSONArray;->length()I

    move-result v0

    if-ge v3, v0, :cond_b

    .line 124
    invoke-virtual {v1, v3}, Lorg/json/JSONArray;->getJSONObject(I)Lorg/json/JSONObject;

    move-result-object v4

    .line 125
    invoke-interface {v2, v3}, Ljava/util/List;->get(I)Ljava/lang/Object;

    move-result-object v0

    check-cast v0, Lcom/facebook/orca/protocol/BatchOperation;

    .line 126
    invoke-virtual {v0}, Lcom/facebook/orca/protocol/BatchOperation;->a()Lcom/facebook/orca/protocol/ApiMethod;

    move-result-object v5

    .line 128
    new-instance v6, Lcom/facebook/orca/protocol/CustomResponseHandlerForBatch;

    invoke-direct {v6}, Lcom/facebook/orca/protocol/CustomResponseHandlerForBatch;-><init>()V

    .line 129
    invoke-virtual {v6, v4}, Lcom/facebook/orca/protocol/CustomResponseHandlerForBatch;->a(Lorg/json/JSONObject;)Ljava/lang/String;

    move-result-object v4

    .line 130
    new-instance v7, Lcom/facebook/orca/protocol/ApiResponse;

    invoke-virtual {v6}, Lcom/facebook/orca/protocol/CustomResponseHandlerForBatch;->b()I

    move-result v8

    invoke-virtual {v6}, Lcom/facebook/orca/protocol/CustomResponseHandlerForBatch;->a()Ljava/util/List;

    move-result-object v6

    invoke-direct {v7, v8, v6, v4}, Lcom/facebook/orca/protocol/ApiResponse;-><init>(ILjava/util/List;Ljava/lang/String;)V

    .line 134
    invoke-virtual {v0}, Lcom/facebook/orca/protocol/BatchOperation;->b()Ljava/lang/Object;

    move-result-object v4

    invoke-interface {v5, v4, v7}, Lcom/facebook/orca/protocol/ApiMethod;->a(Ljava/lang/Object;Lcom/facebook/orca/protocol/ApiResponse;)Ljava/lang/Object;

    move-result-object v4

    .line 135
    invoke-virtual {v0}, Lcom/facebook/orca/protocol/BatchOperation;->c()Ljava/lang/String;

    move-result-object v0

    invoke-virtual {p0, v0, v4}, Lcom/facebook/orca/protocol/RealBatchRunner;->a(Ljava/lang/String;Ljava/lang/Object;)V

    .line 123
    add-int/lit8 v0, v3, 0x1

    move v3, v0

    goto :goto_3

    .line 137
    :cond_b
    return-void
.end method
